package h0908;

import java.util.HashMap;
import java.util.HashSet;

public class FindLongestSubString {
    public static void main(String[] args) {
        String s = "aabbccc";
        System.out.println(getRes(s));
    }

    private static int getRes(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int start = 0;
        int maxLen = 0;
        for (int end = 0; end < s.length(); end++) {
            char c = s.charAt(end);
            map.put(c, map.getOrDefault(c, 0) + 1);
            while (map.size()>2) {
                char ch = s.charAt(start);
                map.put(ch,map.get(ch)-1);
                if (map.get(ch)==0){
                    map.remove(ch);
                }
                start++;
            }
            maxLen = Math.max(maxLen, end - start + 1);
        }
        return maxLen;
    }

}
